/*
 * MIT License
 *
 * Copyright (c) 2020 wen.gu <454727014@qq.com>
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

/***************************************************************************
 * Name: xsession.h
 *
 * Purpose: session base API define and implementation
 *
 * Developer:
 *   wen.gu , 2023-11-02
 *
 * TODO:
 *
 ***************************************************************************/

#ifndef __SHMIPC_COM_XSESSION_H__
#define __SHMIPC_COM_XSESSION_H__

#include <string>
#include <functional>

#include "shmipc/core/core_types.h"
#include "shmipc/core/xresult.h"
#include "shmipc/core/xthread.h"

#include "shmipc/com/com_types.h"
#include "shmipc/com/xmessage.h"

namespace shmipc::com::internal {

class UdsSession;
using UdsSessionSp = std::shared_ptr<UdsSession>;

class UdsSession: public std::enable_shared_from_this<UdsSession> {
public:
    using XResultCode = core::XResultCode;
    using OnReceiveHandler = std::function<void(XMessageSp msg)>;
    using OnErrorHandler = std::function<void(core::XErrC err)>;
    using IoWorker = std::unique_ptr<core::XThread>;
    using OnSessionReleaseHandler = std::function<void(UdsSessionSp)>; //todo refine me??
public:
    virtual ~UdsSession();
    UdsSession(XComId session_id, const std::string& session_name, OnSessionReleaseHandler on_release = nullptr);
public:
    virtual XResultCode start();
    virtual XResultCode stop();

public:
    virtual XComId session_id() const { return session_id_; }
    virtual const std::string& session_name() const { return session_name_; }
    virtual XResultCode reset(XComId session_id); /**  first clean send and receive queue, then update session id */
    virtual void setOnRecvHandler(OnReceiveHandler on_handler);
    virtual void setOnErrorHandler(OnErrorHandler on_handler);
public:
    virtual XResultCode send(XMessageSp msg);
protected:
    virtual void onSendWorker();
    virtual void onRecvWorker();
protected:
    bool is_running_ = false;
    XComId session_id_ = XBUS_INVALID_XCOM_ID;
    std::string session_name_;
    XMessageQueueSp send_queue_ = nullptr;    
    IoWorker recv_worker_ = nullptr;
    IoWorker send_worker_ = nullptr;
    OnReceiveHandler on_recv_handler_ = nullptr;
    OnErrorHandler on_err_handler_ = nullptr;
    OnSessionReleaseHandler on_release_handler_ = nullptr;
};



} //namespace shmipc::com::internal


#endif /** !__SHMIPC_COM_XSESSION_H__ */